package com.liu.eduservice.controller;


import com.liu.commonutils.ReturnResult;
import com.liu.eduservice.entity.EduChapter;
import com.liu.eduservice.entity.chapter.ChapterVo;
import com.liu.eduservice.service.EduChapterService;
import com.liu.servicebase.exceptionhandler.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @since 2022-05-20
 */
@RestController
@RequestMapping("/eduservice/chapter")
@CrossOrigin
@Api(description = "课程章节管理")
public class EduChapterController {

    @Autowired
    EduChapterService eduChapterService;

    // 根据(EduChapter的id号)课程id查询所有数据(章节中包含了小节)
    @GetMapping("/getChapterVideo/{courseId}")
    public ReturnResult queryChapter(@PathVariable String courseId) {
        List<ChapterVo> list = eduChapterService.getChapterVideoByCourseId(courseId);
        return ReturnResult.ok().data("allChapterVideo", list);
    }

    // 增加课程信息
    @ApiOperation("添加章节信息")
    @PostMapping("/addChapterInfo")
    public ReturnResult addChapterInfo(@RequestBody EduChapter eduChapter) {
        try {
            boolean save = eduChapterService.save(eduChapter);
            if (!save) {
                return ReturnResult.error();
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return ReturnResult.ok();
    }

    // 根据章节id删除课程信息
    @DeleteMapping("/deletedChapterInfo/{chapterId}")
    @ApiOperation("根据章节id删除课程信息")
    public ReturnResult deletedChapterInfo(@PathVariable String chapterId) {
        // 一.章节下面没有小节，直接删除
        /*二.章节下面有小节
        * 1.把章节和小节直接删除
        * 2.章节和小节都不删除*/
        try {
            boolean deleteChapterInfo = eduChapterService.deleteChapter(chapterId);
            if (!deleteChapterInfo) {
                return ReturnResult.error();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ReturnResult.ok();
    }

    // 根据章节id查询数据
    @GetMapping("/queryChapterInfoById/{chapterId}")
    @ApiOperation("根据章节id查询章节数据")
    public ReturnResult getChapterInfoById(@PathVariable String chapterId) {
        EduChapter eduChapter = eduChapterService.getById(chapterId);
        return ReturnResult.ok().data("chapter", eduChapter);
    }


    // 根据上面查询chapter信息，进行数据修改
    @PostMapping("/updateChapter")
    @ApiOperation("修改章节信息")
    public ReturnResult updateChapterById(@RequestBody EduChapter eduChapter) {
        try {
            boolean updateById = eduChapterService.updateById(eduChapter);
            if (!updateById) {
                return ReturnResult.error();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ReturnResult.ok();
    }

}

